
#include "RtBase.h"
#include "RtHttpInterface.h"
#include "RtChannelHttpBase.h"
#include "RtChannelHttpClient.h"
#include "RtChannelHttpServerAcceptor.h"
#include "RtHttpUrl.h"
#include "RtHttpAuthInfoGetter.h"

//////////////////////////////////////////////////////////////////////
// class CRtChannelManager
//////////////////////////////////////////////////////////////////////

CRtChannelManager CRtChannelManager::s_rtChannelManagerSingleton;

CRtChannelManager::CRtChannelManager()
{
}

CRtChannelManager* CRtChannelManager::Instance()
{
	return &s_rtChannelManagerSingleton;
}

RtResult CRtChannelManager::
CreateChannelHttpClient(IRtChannelHttpClient *&aClient, 
						CRtHttpUrl *aURL, CFlag aFlag)
{
	RT_ASSERTE_RETURN(aURL, RT_ERROR_INVALID_ARG);
	RT_ASSERTE(!aClient);
	
	IRtChannelHttpClient *pHttpClient = NULL;
	if (RT_BIT_ENABLED(aFlag, CRtChannelManager::CFLAG_WITH_BROWER_PROXY) && 
		aURL->GetScheme() == CRtHttpUrl::s_pszSchemeHttp)
	{
		pHttpClient = new CRtChannelHttpClientWithBrowerProxy(aURL, aFlag);
	}
	else
		pHttpClient = new CRtChannelHttpClient(aURL, aFlag);
	if (!pHttpClient)
		return RT_ERROR_OUT_OF_MEMORY;

	aClient = pHttpClient;
	aClient->AddReference();
	return RT_OK;
}

RtResult CRtChannelManager::
CreateChannelHttpClientWithProxy(IRtChannelHttpClient *&aClient, 
								 CRtHttpUrl *aURL, CRtHttpProxyInfo *aPi)
{
	RT_ASSERTE(!aClient);
	IRtChannelHttpClient *pHttpClient = new CRtChannelHttpClient(aURL, aPi);
	if (!pHttpClient)
		return RT_ERROR_OUT_OF_MEMORY;

	aClient = pHttpClient;
	aClient->AddReference();
	return RT_OK;
}

RtResult CRtChannelManager::
CreateHttpAcceptor(IRtChannelServerAcceptor *&aAcceptor, CFlag aFlag)
{
	RT_ASSERTE(!aAcceptor);
	CRtChannelHttpServerAcceptor *pServer = new CRtChannelHttpServerAcceptor(aFlag);
	if (!pServer)
		return RT_ERROR_OUT_OF_MEMORY;

	aAcceptor = pServer;
	aAcceptor->AddReference();
	return RT_OK;
}

RtResult CRtChannelManager::
CreateUrl(CRtHttpUrl *&aUrl, const CRtString &aSepc)
{
	RT_ASSERTE(!aUrl);
	CRtHttpUrl *pUrlNew = new CRtHttpUrl();
	if (!pUrlNew)
		return RT_ERROR_OUT_OF_MEMORY;

	if (!pUrlNew->Init(aSepc)) {
		delete pUrlNew;
		return RT_ERROR_FAILURE;
	}
	else {
		aUrl = pUrlNew;
		aUrl->AddReference();
		return RT_OK;
	}
}

/*
RtResult CRtChannelManager::
SetHttpProxyUsernameAndPassword(LPCSTR pszUserName, LPCSTR pszPassword)
{
	CRtHttpAuthInfoGetterByUpperLayerSingleton::Instance()->
		SetUserNameAndPassword(pszUserName, pszPassword);
	return RT_OK;
}
*/
